package uk.co.douglashoskins.weathercompare.services;

import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

import uk.co.douglashoskins.weathercompare.WeatherCompare;

public class LocationService {
	private LocationProvider lp = null;
	public LocationService() throws SecurityException, LocationException {
		if (WeatherCompare.NOKIA_LOCATION) {
			lp = NokiaLocationService.getLocationProvider();
		}
		if (lp == null) {
			lp = LocationProvider.getInstance(null);
		}
	}
	
	public double[] getPos() {
		try {
			Location l = lp.getLocation(-1);
			QualifiedCoordinates qc = l.getQualifiedCoordinates();
			return new double[] {qc.getLatitude(), qc.getLongitude()};
		} catch (Exception e) {
			try {
				Location l = LocationProvider.getLastKnownLocation();
				if (l != null) {
					QualifiedCoordinates qc = l.getQualifiedCoordinates();
					return new double[] {qc.getLatitude(), qc.getLongitude()};
				}
			} catch (Exception e2) {
				return null;
			}
		}
		return null;
	}
}
